<?php

/*
 *  ===============================================
 *  cKuler クラス Ver.20091213_1734
 * ===============================================
 * for Kuler API detail see: http://learn.adobe.com/wiki/display/kulerdev/B.+Feeds
 *
 */

if( !Class_Exists('cKuler') ){

class cKuler {
    protected $_sApiKey;
    protected $_sBaseUri;
    protected $_sListType;     /* One of the strings recent (the default), popular, rating, or random. */
    protected $_sStartIndex;   /* A 0-based index into the list that specifies the first item to display. Default is 0, which displays the first item in the list. */
    protected $_sItemsPerPage; /* The maximum number of items to display on a page, in the range 1..100. Default is 20. */
    protected $_sTimeSpan;     /* Value in days to limit the set of themes retrieved. Default is 0, which retrieves all themes without time limit. */

    public function __construct($mApiKey){
        $this->cKuler($mApiKey);
    }

    function cKuler($mApiKey){
        $this->_setDefaultProperty();

        if( Is_Scalar($mApiKey) ){ $this->_setApiKey($mApiKey); }
            else if( Is_Array($mApiKey) ){ $this->_setProperty($mApiKey); }
                else{ return "ERROR while creating class cKuler."; }
    }

    public function get($sApiKey=""){
        //クラスのメッソッドに直接アクセスして来た場合に備えて
        if( !Empty($sApiKey) ){
            $this->cKuler($sApiKey);
        }

        if( IsSet($this->_sApiKey) ){
            $sResult = @file_get_contents( $this->_makeGetQuery() );
            $aResult = (array) simplexml_load_string( $sResult );
            return $this->_getItems( $aResult );
        }
    }

    public function search($mQuery){

        if( IsSet($this->_sApiKey) && !Empty($mQuery)){
            $sResult = @file_get_contents( $this->_makeSearchQuery($mQuery) );
            $aResult = (array) simplexml_load_string( $sResult );
            return $this->_getItems( $aResult );
        }
    }

    public function getApiKey(){
        return $this->_sApiKey;
    }

    public function getProperty(){
        return array(
            "ApiKey"       => $this->_sApiKey,
            "ListType"     => $this->_sListType,
            "StartIndex"   => $this->_sStartIndex,
            "ItemsPerPage" => $this->_sItemsPerPage,
            "TimeSpan"     => $this->_sTimeSpan
            );
    }

    public function setListType($s){
        if( !Is_Scalar($s) ) return;
        $s = strtolower($s);
        switch($s){
            case 'recent':
                $this->_sListType = 'recent'; break;
            case 'popular':
                $this->_sListType = 'popular'; break;
            case 'rating':
                $this->_sListType = 'rating'; break;
            case 'random':
                $this->_sListType = 'random'; break;
            default:
                /* use $this->_sListType as is. */
                break;
        }
    }

    public function setStartIndex($s){
        if( !Is_Scalar($s) ) return;
        if( preg_match_all("/([0-9])+/",$s,$aResult) ){
            $this->_sStartIndex = (int) $aResult[0][0];
        }
        else{ /* use $this->_sStartIndex as is. */ }
    }

    public function setItemsPerPage($s){
        if( !Is_Scalar($s) ) return;
        if( preg_match_all("/([0-9])+/",$s,$aResult) ){
            $iResult = (int) $aResult[0][0];
            $this->_sItemsPerPage = ( $iResult > 100) ? 100 : ( $iResult < 1) ? 1 : $iResult;
        }
        else{ /* use $this->_sItemsPerPage as is. */ }
    }

    public function setTimeSpan($s){
        if( !Is_Scalar($s) ) return;
        if( preg_match_all("/([0-9])+/",$s,$aResult) ){
            $this->_sTimeSpan = (int) $aResult[0][0];
        }
        else{ /* use $this->_sTimeSpan as is. */ }
    }

    /**
     * ===========================
     *  private section
     * =========================== */

    private function _setApiKey($sApiKey){
        if( preg_match_all("/([A-Z0-9])+/",$sApiKey,$aResult) ){
            $this->_sApiKey = (string) $aResult[0][0];
        }
        else{
            $this->_sApiKey = "invalid_apikey";
        }
    }

    private function _setDefaultProperty(){
        $this->_sBaseUri      = 'http://kuler-api.adobe.com/rss';
        $this->_sListType     = 'recent';
        $this->_sStartIndex   = '0';
        $this->_sItemsPerPage = '20';
        $this->_sTimeSpan     = '0';
    }

    private function _makeGetQuery(){
        $sQuery = $this->_sBaseUri . "/get.cfm?key={$this->_sApiKey}&listType={$this->_sListType}&startIndex={$this->_sStartIndex}&itemsPerPage={$this->_sItemsPerPage}&timeSpan={$this->_sTimeSpan}";
        
        return $sQuery;
    }

    private function _makeSearchQuery($sSearchQuery){
        //TODO:escape $sSearchQuery
        $sSearchQuery = $sSearchQuery;
        $sQuery = $this->_sBaseUri . "/search.cfm?searchQuery={$sSearchQuery}&key={$this->_sApiKey}&startIndex={$this->_sStartIndex}&itemsPerPage={$this->_sItemsPerPage}";
        
        return $sQuery;
    }

    private function _setProperty($aProperty){
        if( Is_Array($aProperty)){
            $this->_setApiKey( $aProperty['ApiKey'] );
            $this->setListType( $aProperty['ListType'] );
            $this->setStartIndex( $aProperty['StartIndex'] );
            $this->setItemsPerPage( $aProperty['ItemsPerPage'] );
            $this->setTimeSpan( $aProperty['TimeSpan'] );
        }
    }

    private function _getItemInfo($mItem){
        if( Is_Object($mItem) ) $mItem = (array) $mItem;
        if( Is_Array($mItem) ){
            $sDescription = StrToUpper( $mItem['description'] );

            //strip spaces,\r,\n
            $sReg = '/[\r\n\t ]/';
            $sResult_trimed = preg_replace($sReg,"",$sDescription);

            //FIND HEX RGB
            $sReg = '/HEX:[0-9A-Z,]+/';
            if( preg_match_all($sReg,$sResult_trimed,$aResultWithHEX) ){
                $sReg = '/[0-9A-Z]{6}/';
                if( preg_match_all($sReg,$aResultWithHEX[0][0],$aResult) ){
                    return $aResult[0];
                }
            }
            
        }
    }

    private function _getItems($aItems){
        $aChannel = (array) $aItems['channel'];
        foreach( $aChannel['item'] as $oItem ){
            $aResult[] = $this->_getItemInfo( $oItem );
        }

        return $aResult;

    }
}

}

?>